<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 任务返修单 服务控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TaskRepairOrderService;
use think\Exception;
use think\Request;
use think\Response;
use Throwable;

class TaskRepairOrderController extends VerifyController
{
    /** @var TaskRepairOrderService $taskRepairOrderService */
    protected $taskRepairOrderService;

    /**
     * ContractTemplateController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->taskRepairOrderService = new TaskRepairOrderService();
    }

    /**
     * 创建返修单
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function create(Request $request): Response
    {
        try {
            $res = $this->taskRepairOrderService->create($request->param()['data']);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 开始返修
     * @param Request $request
     * @return Response
     * @throws Exception|Throwable
     */
    public function start(Request $request): Response
    {
        try {
            $res = $this->taskRepairOrderService->start($request->param()['param'], $request->getUserId());
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
        return json(success_response('OK', $res));
    }

    /**
     * 查询任务关联的返修单
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function GetByTaskId(Request $request): Response
    {
        try {
            $res = $this->taskRepairOrderService->GetByTaskId($request->param()['param']['task_id'], $request->getUserId(), $request->param()['param']);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 返修环节
     * @param Request $request
     * @return Response
     */
    public function getRepairStepCategory(Request $request): Response
    {
        $res = $this->taskRepairOrderService->getRepairStepCategory($request->param()['param']['task_id']);
        return json(success_response('OK', $res));
    }

    /**
     * 查询实体返修工序列表
     * @param Request $request
     * @return Response
     */
    public function getEntityRepairStepList(Request $request)
    {
        $param = $request->param()['param'];
        $currentTaskId = $param['current_task_id'];
        $wantRepairEntityId = $param['want_repair_entity_id'];
        $res = $this->taskRepairOrderService->getEntityRepairStepList($currentTaskId, $wantRepairEntityId);
        return json(success_response('OK', $res));
    }
}
